//
//  PrimaryClassificationView.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/20.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "PrimaryClassificationView.h"

@interface PrimaryClassificationView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray <ProClassificationModel*>*dataArray;

@end

@implementation PrimaryClassificationView

+ (CGFloat)calculateHeight {
    
    CGFloat collectionHeight = (TTScreenWidth/5.5)*1.3;
    return collectionHeight;
}

- (void)fillWithArray:(NSMutableArray <ProClassificationModel *>*)dataArray {
    self.dataArray = dataArray;
    [self.collectionview reloadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];

    [self initView];
}

- (void)initView {
    
    CGFloat width = TTScreenWidth;
    
    CGFloat cellWidth;
    cellWidth = width/5.5 - 4;
    
    UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc]init];
    flowLaout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    flowLaout.minimumInteritemSpacing = 5;
    flowLaout.minimumLineSpacing = 5;
    
    flowLaout.itemSize = CGSizeMake(cellWidth, cellWidth*1.2);

    self.collectionview.collectionViewLayout = flowLaout;
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.showsHorizontalScrollIndicator = NO;
    self.collectionview.showsVerticalScrollIndicator = NO;
    
    [self.collectionview registerNib:[UINib nibWithNibName:@"PrimaryClassificationCell" bundle:nil] forCellWithReuseIdentifier:@"PrimaryClassificationCellID"];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PrimaryClassificationCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PrimaryClassificationCellID" forIndexPath:indexPath];
//    cell.backgroundColor = UIColor.blackColor;
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}
    
    

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (self.PrimaryClassificationViewBlock) {
        self.PrimaryClassificationViewBlock(indexPath.row);
    }
}

- (NSMutableArray<ProClassificationModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

@end
